package com.aaa.ssm.controller;

import com.aaa.ssm.util.AjaxResult;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("upload")
public class FileController {

    @Value("${haha.baseurl}")
    private  String BASE_URL ;
    @Value("${haha.dir}")
    private  String STORE_DIR ;
    @PostMapping
    public AjaxResult uploadOSS(MultipartFile file) throws Exception{
        // 设置 OSS Endpoint 和 Bucket 名称
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 替换为您的 Bucket 区域
        String region = "cn-hangzhou";
        // 创建 OSSClient 实例
        String accessKeyId = "";
        String accessKeySecret = "";

        // 使用 DefaultCredentialProvider 简化凭证提供者设置
        DefaultCredentialProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .region(region)
                .build();

        String originalFilename = file.getOriginalFilename();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String randomFileName = UUID.randomUUID().toString() + fileExtension;

        String  bucketName = "qy181";

        ossClient.putObject(bucketName, randomFileName, file.getInputStream());

        String accessUrl = "https://qy181.oss-cn-hangzhou.aliyuncs.com/" + randomFileName;

        return AjaxResult.success(200, "ok", accessUrl);
    }



    @PostMapping("uploadLocal")
    public AjaxResult upload(MultipartFile file){
        if (file.isEmpty()) {
            return AjaxResult.error(400, "上传文件为空");
        }
        try {
            String originalFilename = file.getOriginalFilename();
            String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String randomFileName = UUID.randomUUID().toString() + fileExtension;
            String filePath = STORE_DIR + randomFileName;
            File dest = new File(filePath);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            // 使用 FileCopyUtils.copy 方法进行文件复制
            FileCopyUtils.copy(file.getInputStream(), new java.io.FileOutputStream(dest));
            String accessUrl = BASE_URL + randomFileName;
            return AjaxResult.success(200, "ok", accessUrl);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(500, "文件上传失败");
        }
    }
}
